package com.example.fairplay.newfpregistration.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.fairplay.newfpregistration.R;
import com.example.fairplay.newfpregistration.util.StatusBarCompat;
import com.example.mylibrary.config.Config;
import com.example.mylibrary.util.Logger;
import com.example.mylibrary.util.PreferencesUtils;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.aratek.facelib.AraErrorCode;
import cn.com.aratek.facelib.AraFaceAuthLib;
import cn.com.aratek.facelib.FaceAuthListeners;


public class MainMenu extends Activity {
	private static final int SHOW_ARASERIVICE_DIALOG = 0;
	private static final int DISMISS_ARASERIVICE_DIALOG = 1;
	private ExecutorService executorService;
	private static AraFaceAuthLib araFaceAuthLib;
	private ProgressDialog mProgressDialog;
	public Logger logger;
	private boolean isOpenWifi = false;
	private WifiManager wm;
	@Bind(R.id.text_title)
	TextView tvTitle;
	@Bind(R.id.tv_back)
	TextView tvBack;
	@Bind(R.id.button_backward)
	Button btnBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_menu);
		StatusBarCompat.compat(this, Color.rgb(7, 136, 242));
		ButterKnife.bind(this);
		initData();
		logger = new Logger();
		//新建一个File，传入文件夹目录
		File file = new File("/mnt/sdcard/FP-KTA2203/img");
		//判断文件夹是否存在，如果不存在就创建，否则不创建
		if (!file.exists()) {
			//通过file的mkdirs()方法创建<span style="color:#FF0000;">目录中包含却不存在</span>的文件夹
			file.mkdirs();
		}

	}

	private void initData() {
		tvTitle.setText("人证检录系统");
		btnBack.setVisibility(View.GONE);
		tvBack.setVisibility(View.GONE);
		//亚略特是否授权
		wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
		executorService = Executors.newCachedThreadPool();
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setIcon(android.R.drawable.ic_dialog_info);
		mProgressDialog.setIndeterminate(false);
		mProgressDialog.setCancelable(false);
		araFaceAuthLib = new AraFaceAuthLib(this);
		//     showProgressDialog();
		executorService.execute(new Runnable() {
			@Override
			public void run() {
				timer.start();
				araFaceAuthLib.setStatusListener(listener);
				araFaceAuthLib.bind();
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();


	}

	private boolean card_face_finger;
	private boolean face;
	private boolean card_finger;

	@OnClick({R.id.ry_butten, R.id.ry_butten1, R.id.ry_butten2, R.id.ry_butten3})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.ry_butten:
				card_face_finger = PreferencesUtils.getBoolean(Config.SETTING_CARD_FACE_FINGER, true);
				face = PreferencesUtils.getBoolean(Config.SETTING_FACE, false);
				card_finger = PreferencesUtils.getBoolean(Config.SETTING_CARD_FINGER, false);

				Intent intent = null;
				if (face) {
					intent = new Intent(this, FaceToManyActivity.class);
				} else {
					intent = new Intent(this, ProjectPickActivity.class);
				}

				startActivity(intent);

				break;
			case R.id.ry_butten1:

				Intent intent2 = new Intent(this, RecordItemActivity2.class);

				startActivity(intent2);
				break;
			case R.id.ry_butten2:
				Intent intent3 = new Intent(this, SystemSettingActivity.class);
				startActivity(intent3);
				break;
			case R.id.ry_butten3:
				android.os.Process.killProcess(android.os.Process.myPid());
				System.exit(0);
				break;

		}
	}

	public static AraFaceAuthLib getAraFaceAuthLib() {
		return araFaceAuthLib;

	}

	private CountDownTimer timer = new CountDownTimer(10000, 1000) {

		@Override
		public void onTick(long millisUntilFinished) {

		}

		@Override
		public void onFinish() {
			timer.cancel();
			dismissProgressDialog();
		}
	};

	private FaceAuthListeners.StatusListener listener = new FaceAuthListeners.StatusListener() {

		@Override
		public void onServiceDisConnected() {
			logger.e("onServiceDisConnected");
			timer.onFinish();
		}

		@Override
		public void onServiceConnected() {
			araFaceAuthLib.setDebug(true);
			mProgressDialog.setMessage("正在初始化...");

			if (!wm.isWifiEnabled()) {
				isOpenWifi = false;
				toggleWiFi(MainMenu.this, true);
				try {
					Thread.sleep(1500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} else {
				isOpenWifi = true;
			}
			int a = araFaceAuthLib.initFaceEngine(270);
			if (a != AraErrorCode.ARA_OK) {
				a = araFaceAuthLib.initFaceEngine(270);
			}
			if (!isOpenWifi) {
				toggleWiFi(MainMenu.this, false);
			}
			String msg = (a == AraErrorCode.ARA_OK) ? "核心初始化成功" : "核心初始化失败";
			Toast.makeText(MainMenu.this, msg, Toast.LENGTH_SHORT).show();
			timer.onFinish();
		}
	};

	private void toggleWiFi(Context context, boolean enabled) {
		wm.setWifiEnabled(enabled);
	}

	@Override
	protected void onDestroy() {
		araFaceAuthLib.cleanListeners();
		araFaceAuthLib.releaseFaceEngine();
		super.onDestroy();
	}


	private void showProgressDialog() {
		mProgressDialog.setTitle("");
		mProgressDialog.setMessage("正在绑定服务...");
		mProgressDialog.show();
	}

	private void dismissProgressDialog() {
		mHandler.sendEmptyMessage(DISMISS_ARASERIVICE_DIALOG);
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case SHOW_ARASERIVICE_DIALOG:
					mProgressDialog.setTitle("");
					mProgressDialog.setMessage("正在绑定服务...");
					mProgressDialog.show();
					break;
				case DISMISS_ARASERIVICE_DIALOG:
					mProgressDialog.dismiss();
					break;
			}

		}

		;
	};

}
